﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizzCommInterfaces;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace XMLTrays
{
    public class XMLOutbox : Tray, IOutbox
    {
        //private const string _name = "XMLOutboxProvider";
        private const char _separator = ';';
        public string folder { get; set; }
       
        //private string concreteOutboxName;

        public XMLOutbox()
        {
            folder = null;
            base._name = "XMLOutboxProvider";
        }

        
        #region IOutbox Members

        public void getConfigurationWindow()
        {
            new XMLOutboxConfigControl(this).ShowDialog();
            
        }

        public int getCount()
        {
            var files = Directory.GetFiles(folder).Where(name => name.EndsWith(".xml"));
            return files.Count();
        }

        public void writeMessage(BizzMessage msg)
        {
            
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
            doc.AppendChild(dec);

            XmlElement root = doc.CreateElement("mensaje");
            doc.AppendChild(root);
            root.SetAttribute("titulo", msg.Title);
            XmlElement attributes = doc.CreateElement("atributos");
            root.AppendChild(attributes);
            foreach (MsgAttribute attrib in msg.Attributes)
            {
                XmlElement attributeNode = doc.CreateElement("atributo");
                attributeNode.SetAttribute("nombre", attrib.Name);
               /* switch (attrib.Type)
                {
                    case BizzCommInterfaces.Type.boolean: attributeNode.SetAttribute("tipo", "Booleano"); break;
                    case BizzCommInterfaces.Type.date: attributeNode.SetAttribute("tipo", "Fecha"); break;
                    case BizzCommInterfaces.Type.number: attributeNode.SetAttribute("tipo", "Numero"); break;
                    case BizzCommInterfaces.Type.text: attributeNode.SetAttribute("tipo", "Texto"); break;
                    
                }*/

                attributeNode.SetAttribute("tipo", msg.setMsgType(attrib.Type));
                
                attributeNode.SetAttribute("valor", attrib.Value);
                attributes.AppendChild(attributeNode);
            }
            doc.Save(folder + "\\" + string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}_", DateTime.Now) + msg.Title + ".xml");
        }



        #endregion

       

        public string[] getDescription()
        {
            string[] ret = new string[9];
            ret[0] = "Cada mensaje será un archivo XML con el siguiente formato:";
            ret[1] = "";
            ret[2] = "\"<mensaje titulo=\"este es el titulo del mensaje\">";
            ret[3] = "<atributos>   ";
            ret[4] = "<atributo nombre=\"Apellido\" tipo=\"Texto\" valor=\"Perez\"/> ";
            ret[5] = "<atributo nombre=\"Edad\" tipo=\"Numero\" valor=\"30\"/> ";
            ret[6] = "<atributo nombre=\"Casado\" tipo=\"Booleano\" valor=\"True\"/>  ";
            ret[7] = "</atributos> ";
            ret[8] = "</mensaje> ";


            return ret;
        }

       

   

        #region IOutbox Members

        public string codeMe()
        {
            string ret = folder + _separator +  _concreteName;
            return ret;
        }

        public void decodeMe(string box)
        {
            string[] attr = box.Split(_separator);
            folder = attr[0];
            _concreteName = attr[1];
        }

        public bool isReady()
        {
            return (folder != null);
        }

        #endregion

        #region IOutbox Members


        public string getConcreteOutboxName()
        {
            return _concreteName;
        }

        public string getOutboxName()
        {
            return _name;
        }

        public void setConcreteOutboxName(string name)
        {
            _concreteName = name;
        }

        #endregion
    }
}
